package au.com.betasoftware.vhregs;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements TextWatcher {
	
    private TextView mark;
    private TextView manufacturer;
    private TextView operator;
    private TextView model;
    
	private ProgressDialog dialog;
	
	private EditText regoInput;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        regoInput = (EditText) findViewById(R.id.editText1);
        regoInput.addTextChangedListener(this);
        
    	mark = (TextView) findViewById(R.id.textView1);
    	manufacturer = (TextView) findViewById(R.id.textView4);
    	operator = (TextView) findViewById(R.id.textView6);
    	model = (TextView) findViewById(R.id.textView8);
    	
    	mark.setVisibility(View.INVISIBLE);
    	manufacturer.setVisibility(View.INVISIBLE);
    	operator.setVisibility(View.INVISIBLE);
    	model.setVisibility(View.INVISIBLE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void lookupReg(View view) {
    	String message = regoInput.getText().toString();
   		new AirplaneDetails().execute(message);
    }
    
    private class AirplaneDetails extends AsyncTask<String, Void, JSONObject> {

    	@Override
    	protected JSONObject doInBackground(String... params) {
    		 try {
		        InputStream inputStream = null;
		        String asString = null;
			 
	        	URL url = new URL("http://planespotter.cloudfoundry.com/api/aircraft/" + params[0]);
	        	HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        	connection.connect();
	        	inputStream = connection.getInputStream();
	        	
	        	BufferedReader bufferedReader = new BufferedReader
	        			(new InputStreamReader(inputStream, "ISO-8859-1"),8);
	        	StringBuilder builder = new StringBuilder();
	        	String line = null;
	        	while((line = bufferedReader.readLine()) != null) {
	        		builder.append(line +"\n");
	        	}
	        	inputStream.close();
	        	asString = builder.toString();
	        	return new JSONObject(asString); 
	        } catch(Exception e) {
	        	e.printStackTrace();
	        	return null;
	        }
    	}
    	
    	@Override
    	protected void onPostExecute(JSONObject result) {
    		super.onPostExecute(result);
            try {
    			mark.setText(result.getString("mark"));
    			manufacturer.setText(result.getString("manufacturer"));
    			model.setText(result.getString("model"));
    			operator.setText(result.getString("operatorName"));
    			
    			mark.setVisibility(View.VISIBLE);
    			manufacturer.setVisibility(View.VISIBLE);
    			model.setVisibility(View.VISIBLE);
    			operator.setVisibility(View.VISIBLE);
    			
    			regoInput.setText("");
    			dialog.dismiss();
    		} catch (JSONException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	
    	@Override
    	protected void onPreExecute() {
    		super.onPreExecute();
    		
			mark.setVisibility(View.INVISIBLE);
			manufacturer.setVisibility(View.INVISIBLE);
			model.setVisibility(View.INVISIBLE);
			operator.setVisibility(View.INVISIBLE);
    		
    		dialog = ProgressDialog.show(MainActivity.this, "", "Searching. Please wait...");
    	}

    }

	public void afterTextChanged(Editable s) {
		if(s.toString().length() == 2 && s.toString().equals("VH") ) {
			regoInput.append("-");
		}
	}

	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub
		
	}

	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}
    
}
